var echarts;
var zjj;
$(document).ready(function () {
    require.config({
        baseUrl: ctx + '/resource/plugins/echarts4',
        paths: {
            echarts: 'echarts',
            roma: 'theme/roma'
        }
    });
    require(
        [
            'echarts',
            'roma'
        ],
        function (ec) {
            ECharts = ec;
            queryZjjInfo();//中间件基本信息

            queryZtYbp();//服务状态可用性

            qyeryJkdYbp();//中间件健康度

            zjj_ttl_top = ECharts.init(document.getElementById('zjj_ttl_top'), 'roma');//吞吐率
            queryTulTop();

            zjj_dqdnc_top = ECharts.init(document.getElementById('zjj_dqdnc_top'), 'roma');//当前内存堆使用比率
            queryDqncdTop();
        }
    );
});

/**中间件基本信息*/
function queryZjjInfo() {
    $.ajax({
        url: ctx + "/HostPage/queryTableInfo",
        type: "post",
        async: false,
        data: {CI_TYPE: CI_TYPE, DI_QU: DI_QU},
        success: function (data) {
            var map = JSON.parse(data);
            var list = map.list;
            var list1 = map.list1;
            var tablel = "";
            if (list1 == undefined || list == undefined || list == '' || list1 == '') {
                tablel += "<ul>" +
                    "<li class='first' style='width:33.3%'>" + "分类" + "</li>" +
                    " <li style='width:33.3%'>" + "资源总数" + "</li>" +
                    "<li class='last' style='width:33.3%'>" + "暂无数据" + "</li>" +
                    "</ul>" +
                    "<ul>" +
                    "<li class='first' style='width:33.3%'>" + "数量" + "</li>" +
                    " <li style='width:33.3%'>" + "暂无数据" + "</li>" +
                    "<li class='last' style='width:33.3%'>" + "暂无数据" + "</li>" +
                    "</ul>";
                $("#zzj_info").append(tablel);
            } else {
                zjj = list1[0].ZSL;
                tablel += "<ul>" +
                    "<li class='first' style='width:33.3%'>" + "分类" + "</li>" +
                    " <li style='width:33.3%'>" + "资源总数" + "</li>" +
                    "<li class='last' style='width:33.3%'>" + list[0].CLASSNAME + "</li>" +
                    "</ul>" +
                    "<ul>" +
                    "<li class='first' style='width:33.3%'>" + "数量" + "</li>" +
                    " <li style='width:33.3%'>" + list1[0].ZSL + "</li>" +
                    "<li class='last' style='width:33.3%'>" + list[0].SL + "</li>" +
                    "</ul>";
                $("#zzj_info").append(tablel);
            }
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}

/**服务状态可用性*/
//PM-00-00-000-02
function queryZtYbp() {
    $.ajax({
        url: ctx + "/HostPage/queryYbp",
        type: "post",
        data: {CI_TYPE: CI_TYPE, KPI_CODE: "KPI-Weblogic-all-7"},
        success: function (data) {
            var cpuState = JSON.parse(data);
            if (cpuState == undefined) {
                var qq = "<div class='easy-pie-chart percentage' data-percent='' id='' data-color='#87B87F'style='height: 75px; width: 75px; line-height: 74px; color: rgb(135, 184, 127);'>" +
                    "<span class='percent' style='top:0px;left:40px;'>" + "暂无数据" + "</span>" +
                    "<canvas height='75' width='75'>" + "</canvas>" + "</div>" + "<h3 style='line-height:3;'>" + "可用性" + "</h3>";
                $("#zjj_zt_ybp").append(qq);
            } else {
                var qq = "<div class='easy-pie-chart percentage' data-percent='' id='zjjzt' data-color='#5eadf0'style='height: 75px; width: 75px; line-height: 74px; color: rgb(135, 184, 127);'>" +
                    "<span class='percent' style='top:0px;left:40px;'>" + parseInt(cpuState[0].SUCCESS_VALUE / cpuState[0].COUNT_VALUE * 100) + "%" + "</span>" +
                    "<canvas height='75' width='75'>" + "</canvas>" + "</div>" + "<h3 style='line-height:3;'>" + "可用性" + "</h3>";
                $("#zjj_zt_ybp").append(qq);
                $('#zjjzt').attr('data-percent', parseInt(cpuState[0].SUCCESS_VALUE / cpuState[0].COUNT_VALUE * 100));
            }
            $('.easy-pie-chart.percentage').each(function () {
                $(this).easyPieChart({
                    barColor: $(this).data('color'),
                    trackColor: '#EEEEEE',
                    scaleColor: false,
                    lineCap: 'butt',
                    lineWidth: 8,
                    animate: ace.vars['old_ie'] ? false : 1000,
                    size: 120
                }).css('color', $(this).data('color'));
            });
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });

}

//中间件健康度
function qyeryJkdYbp() {
    $.ajax({
        url: ctx + "/HostPage/queryJkd",
        type: "post",
        data: {CI_TYPE: CI_TYPE, CI_TYPE: CI_TYPE},
        success: function (data) {
            var cpuState = JSON.parse(data);
            if (zjj == undefined) {
                var qq = "<div class='easy-pie-chart percentage' data-percent='' id='' data-color='#87B87F'style='height: 75px; width: 75px; line-height: 74px; color: rgb(135, 184, 127);'>" +
                    "<span class='percent' style='top:0px;left:12px;'>" + "暂无数据" + "</span>" +
                    "<canvas height='75' width='75'>" + "</canvas>" + "</div>" + "<h3 style='line-height:3;'>" + "健康度" + "</h3>";
                $("#zjj_jkd").append(qq);
            } else {
                var qq = "<div class='easy-pie-chart percentage' data-percent='' id='zjjjkd' data-color='#87B87F'style='height: 75px; width: 75px; line-height: 74px; color: rgb(135, 184, 127);'>" +
                    "<span class='percent' style='top:0px;left:40px;'>" + parseInt((zjj - cpuState[0].EXT_UNIT_ID) / zjj * 100) + "%" + "</span>" +
                    "<canvas height='75' width='75'>" + "</canvas>" + "</div>" + "<h3 style='line-height:3;'>" + "健康度" + "</h3>";
                $("#zjj_jkd").append(qq);
                $('#zjjjkd').attr('data-percent', parseInt((zjj - cpuState[0].EXT_UNIT_ID) / zjj * 100));
            }
            $('.easy-pie-chart.percentage').each(function () {
                $(this).easyPieChart({
                    barColor: $(this).data('color'),
                    trackColor: '#EEEEEE',
                    scaleColor: false,
                    lineCap: 'butt',
                    lineWidth: 8,
                    animate: ace.vars['old_ie'] ? false : 1000,
                    size: 120
                }).css('color', $(this).data('color'));
            });
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });

}

//总线程数top榜
//PM-00-04-009-16
function queryTulTop() {
    $.ajax({
        url: ctx + "/HostPage/queryTop",
        type: "post",
        data: {KPI_CODE: "KPI-Weblogic-all-25", DI_QU: DI_QU},
        success: function (data) {
            var map = JSON.parse(data);
            var list = map.list;
            var xdata = [];
            var ydata = [];
            var x_value = [];
            for (var i = 0; i < list.length; i++) {
                x_value = list[i].IP
                xdata.push(x_value);
                ydata.push(list[i].AVG_VALUE);
            }
            if (x_value == undefined || x_value == null || x_value == '') {
                var table = "<h3 style='margin-top: 150px;padding-left: 700px;'>" + "暂无数据" + "</h3>";
                $("#zjj_ttl_top").html(table);
            } else {
                option = {
                    color: ['#3398DB'],
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        }
                    },
                    legend: {
                        formatter: function (series) {
                            return series;
                        },
                        textStyle: {
                            color: '#AAAAAA'
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            // data: [1, 12, 23, 3,],
                            data: xdata,
                            axisTick: {
                                alignWithLabel: true
                            },
                            axisLabel: {
                                interval: 0,
                                rotate: 45,
                                margin: 8
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            // max: 100,
                            splitLine: {
                                show: true,
                            },
                            nameTextStyle: {
                                color: '#000000',
                            },
                            axisLabel: {
                                formatter: '{value} ',
                                textStyle: {
                                    color: '#000000'
                                }
                            }
                        }
                    ],
                    series: [
                        {
                            name: '总线程数',
                            type: 'bar',
                            barWidth: '60%',
                            // color: '#3398DB',
                            itemStyle: {
                                normal: {
                                    color: new ECharts.graphic.LinearGradient(
                                        0, 0, 0, 1,
                                        [
                                            {offset: 0, color: '#acc7f6'},
                                            {offset: 0.5, color: '#5eadf0'},
                                            {offset: 1, color: '#3621f0'}
                                        ]
                                    )
                                }
                            },
                            data: ydata
                        }
                    ]
                };
                zjj_ttl_top.setOption(option);
            }
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}

//当前内存堆使用比率top榜
function queryDqncdTop() {
    $.ajax({
        url: ctx + "/HostPage/queryTop",
        type: "post",
        data: {KPI_CODE: "KPI-Weblogic-all-21", DI_QU: DI_QU},
        success: function (data) {
            var map = JSON.parse(data);
            var list = map.list;
            var xdata = [];
            var ydata = [];
            var x_value = [];
            for (var i = 0; i < list.length; i++) {
                x_value = list[i].IP;
                xdata.push(x_value);
                ydata.push(list[i].AVG_VALUE);
            }
            if (x_value == undefined || x_value == null || x_value == '') {
                var table = "<h3 style='margin-top: 150px;padding-left: 700px;'>" + "暂无数据" + "</h3>";
                $("#zjj_dqdnc_top").html(table);
            } else {
                option = {
                    color: ['#3398DB'],
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                        }
                    },
                    legend: {
                        formatter: function (series) {
                            return series;
                        },
                        textStyle: {
                            color: '#AAAAAA'
                        }
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            // data: [1, 12, 23, 3,],
                            data: xdata,
                            axisTick: {
                                alignWithLabel: true
                            },
                            axisLabel: {
                                interval: 0,
                                rotate: 45,
                                margin: 8
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            // max: 100,
                            splitLine: {
                                show: true,
                            },
                            nameTextStyle: {
                                color: '#000000',
                            },
                            axisLabel: {
                                formatter: '{value} ',
                                textStyle: {
                                    color: '#000000'
                                }
                            }
                        },
                    ],
                    series: [
                        {
                            name: '当前内存堆使用比率',
                            type: 'bar',
                            barWidth: '60%',
                            // color: '#3398DB',
                            itemStyle: {
                                normal: {
                                    color: new ECharts.graphic.LinearGradient(
                                        0, 0, 0, 1,
                                        [
                                            {offset: 0, color: '#acc7f6'},
                                            {offset: 0.5, color: '#5eadf0'},
                                            {offset: 1, color: '#3621f0'}
                                        ]
                                    )
                                },
                            },
                            data: ydata,
                        }
                    ]
                };
                zjj_dqdnc_top.setOption(option);
            }
        }, error: function (textStatus) {
            console.log("error:" + textStatus.status);
        }
    });
}